<?php
namespace app\mobile\controller;

use app\model\Record; 
use app\model\Comm; 
use app\model\Theme; 
use app\model\Share; 

class Yxdanmu extends Base
{
    public function Yxdanmu()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
$config = Theme::getConfig($themeid);
$mode = intval($_P['mode']);
if($config['isdanmu'] == 1){
	$danmulist = array();

	//获取弹幕的条数模式1为首次打开 2为定时刷新
	if ($mode==1){
		$limitNum=10;
	}else{
		$limitNum=30;
	}
	if($config['iscomm']==1){
		$commlist = Comm::getListByThemeId($themeid,$limitNum);
	    if(!empty($commlist)){
            foreach ($commlist as $index => $row){
		    	$data = array(
					'info'   => mb_substr($row['nickname'],0,5,'utf-8').' 刚刚获得红包'.$row['fee'].'元',
					'img'    => $row['avatar'],
					'close'  =>false,
					);
		    	$danmulist[] = $data;
            }
        }
    }
    if($config['isreply'] == 1){
        $replylist =Reply::getListLimit20($themeid);
        if (!empty($replylist)){
		    foreach ($replylist as $index => $row){
		    	$data = array(
					'info'   => mb_substr($row['nickname'],0,5,'utf-8').' 说:'.$row['content'],
					'img'    => $row['avatar'],
					'close'  =>false,
					);
		    	$danmulist[] = $data;
			}
        }
    }
    if($config['isicon'] > 0 && $config['iconnum2'] == 0){
        $slist =Share::getListByThemeId($themeid,5);
        if (!empty($slist)){
			foreach ($slist as $index => $row){
		    	$data = array(
					'info'   =>  mb_substr($row['nickname'],0,5,'utf-8').'刚刚点亮1颗图标',
					'img'    => $row['avatar'],
					'close'  =>false,
					);
		    	$danmulist[] = $data;
			}
		}
    }
    $glist = Record::getLimitByIdnew($themeid,$limitNum);
    if (!empty($glist)){
		foreach ($glist as $index => $row){
			if($row['type'] == 0){
				$row['content'] = mb_substr($row['nickname'],0,5,'utf-8').' 刚刚成功开团';
			}if($row['type'] == 1){
				$row['content'] = mb_substr($row['nickname'],0,5,'utf-8').' 刚刚参与拼团';
			}if($row['type'] == 9){
				$row['content'] = mb_substr($row['nickname'],0,5,'utf-8').' 刚刚抢购成功';
			}
	    	$data = array(
				'info'   => $row['content'],
				'img'    => $row['avatar'],
				'close'  =>false,
				'color'  =>  '#ff6600',
				);
	    	$danmulist[] = $data;
		}
    }
	// if (!empty($fakelist)){
 //        foreach ($fakelist as $index => $row){
 //            $row['content'] = mb_substr($row['nickname'],0,5,'utf-8').' 刚刚参加活动';
 //            $danmulist[] = $row;
 //        }
 //    }
    if (!empty($danmulist)) {
		if($mode === 1 ){
		    foreach ($danmulist as $index => $row){
				$danmulist[$index]['top'] = rand(0, 5)*45+10;
				if ($index>0){
					if ($danmulist[$index]['top']==$danmulist[$index-1]['top']){
						$danmulist[$index]['top'] = rand(0, 5)*45+10;
						if ($danmulist[$index]['top']==$danmulist[$index-1]['top']){
							$danmulist[$index]['top'] = rand(0, 5)*45+10;
						}
					}
				}
			}
			shuffle($danmulist);
			echo json_encode($danmulist);
		}elseif($mode === 2){
		    foreach ($danmulist as $index => $row){
				$danmulist[$index]['top'] = 30;
			}
		    echo json_encode($danmulist[array_rand($danmulist)]);
		}
	}
}
}
}